//
//  GuideMallViewController.m
//  MobileShoppingMall
//
//  Created by 冯传昌 on 2021/4/14.
//  Copyright © 2021 冯传昌. All rights reserved.
//

#import "GuideMallViewController.h"

#import "GuideMallHeaderView.h"
#import "GuideMallCell.h"

#import "MainPageHeaderModel.h"

#import "InviteFriendImgCell.h"

#import "GoodTypesCell.h"

#import "GuideMallModel.h"

@interface GuideMallViewController ()<UIScrollViewDelegate,UITableViewDelegate,UITableViewDataSource>

@property (nonatomic,strong) UITableView *tableView;

@property (nonatomic,strong) GuideMallHeaderView *mallV;

@property (nonatomic,strong) GeneralNavSearchView *topNavView;

@property (nonatomic,strong) NSMutableArray *goodTypesArr;

@property (nonatomic,strong) NSMutableArray *dataSourceArr;

@property (nonatomic,assign) NSInteger currentPage;

@property (nonatomic,assign) BOOL isRefreshing;//当前页面是否正在刷新

@end

@implementation GuideMallViewController


- (void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
    [self loadHeaderData];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.currentPage = 1;
    self.goodTypesArr = [NSMutableArray array];
    self.dataSourceArr = [NSMutableArray array];
    [self initUI];
    [self getNewData];
}

#pragma mark - data
-(void)loadHeaderData{
    NSMutableArray *bannerArr = [NSMutableArray array];
    self.goodTypesArr = [NSMutableArray array];
    WEAKSELF(self);
    [NetWorkingShop getGuideShopInfoWithSuccessBlock:^(id  _Nonnull responseObj) {
        NSDictionary *data = responseObj[@"data"];
        NSArray *types = data[@"types"];
        NSArray *banners = data[@"productcarouses"];
        for (NSDictionary *dic in types) {
            MainPageHeaderModel *model = [MainPageHeaderModel mj_objectWithKeyValues:dic];
            model.Id = [[dic objectForKey:@"id"] integerValue];
            [self.goodTypesArr addObject:model];
        }
        
        for (NSDictionary *dic in banners) {
            MainPageHeaderModel *model = [MainPageHeaderModel mj_objectWithKeyValues:dic];
            model.Id = [[dic objectForKey:@"id"] integerValue];
            [bannerArr addObject:model];
        }
        weakSelf.mallV.bannerSourceArr = bannerArr;
        
        NSIndexSet *indexSet = [[NSIndexSet alloc]initWithIndex:0];
        [weakSelf.tableView reloadSections:indexSet withRowAnimation:UITableViewRowAnimationAutomatic];
    }];
}

-(void)getNewData{
    [NetWorkingShop getGuideShopListWithPage:self.currentPage WithSuccessBlock:^(id  _Nonnull responseObj) {
        self.dataSourceArr = [NSMutableArray array];
        NSDictionary *data = responseObj[@"data"];
        NSArray *records = data[@"records"];
        for (NSDictionary *dic in records) {
            GuideMallModel *model = [GuideMallModel paraWithDict:dic];
            [self.dataSourceArr addObject:model];
        }
        [self.tableView reloadData];
        [self delayLoadRefreshStatus];
    }];
    
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [self delayLoadRefreshStatus];
    });
}
-(void)getMoreData{
    [NetWorkingShop getGuideShopListWithPage:self.currentPage WithSuccessBlock:^(id  _Nonnull responseObj) {
        NSDictionary *data = responseObj[@"data"];
        NSArray *records = data[@"records"];
        for (NSDictionary *dic in records) {
            GuideMallModel *model = [GuideMallModel paraWithDict:dic];
            [self.dataSourceArr addObject:model];
        }
        [self.tableView reloadData];
        [self delayLoadRefreshStatus];
    }];
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [self delayLoadRefreshStatus];
    });
}
//延迟刷新
-(void)delayLoadRefreshStatus{
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5f * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer endRefreshing];
        self.isRefreshing = NO;
    });
}
- (void)setIsRefreshing:(BOOL)isRefreshing{
    _isRefreshing = isRefreshing;
    [self.view setUserInteractionEnabled:!_isRefreshing];
    self.tableView.scrollEnabled = !_isRefreshing;
}
#pragma mark - UI
-(void)initUI{
    [self.view addSubview:self.tableView];
    [self.tableView setTableHeaderView:[self tableHeaderView]];
    [self.view addSubview:self.topNavView];
    
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        if (self.isRefreshing == NO) {
            self.isRefreshing = YES;
            self.currentPage = 1;
            [self.tableView.mj_header beginRefreshing];
            [self getNewData];
        }
        
    }];
    self.tableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
        if (self.isRefreshing == NO) {
            self.isRefreshing = YES;
            self.currentPage = self.currentPage + 1;
            [self.tableView.mj_footer beginRefreshing];
            [self getMoreData];
        }
    }];
}
#pragma mark - lazy
//自定义导航栏view
-(GeneralNavSearchView *)topNavView{
    WEAKSELF(self);
    if (!_topNavView) {
        _topNavView = [[GeneralNavSearchView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, kHeight_NavBar)];
        _topNavView.backgroundColor = UIColor.whiteColor;
        _topNavView.alpha = 0;
        
        [_topNavView setShowMenuBlock:^{
            [weakSelf showMenuClick];
        }];
    }
    return _topNavView;
}
//tableView
-(UITableView *)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, -kHeight_StatusBar-5, ScreenWidth, ScreenHeight-kHeight_NavBar+kHeight_StatusBar+5) style:UITableViewStyleGrouped];
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.estimatedRowHeight = 0;
        _tableView.backgroundColor = UIColor.whiteColor;
    }
    return _tableView;
}
-(UIView *)tableHeaderView{
    WEAKSELF(self);
    self.mallV = [[GuideMallHeaderView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenWidth*0.5)];
    [self.mallV setUpdateHeightBlock:^(CGFloat height) {
        weakSelf.mallV.height = height;
    }];
    return self.mallV;
}
#pragma mark - UITableViewDelegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 2;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (section == 0) {
        return self.goodTypesArr.count > 0 ? 2:1;;
    }else{
        return self.dataSourceArr.count;
    }
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 1) {
        GuideMallModel *model = self.dataSourceArr[indexPath.row];
        return model.height;
    }else{
        if (self.goodTypesArr.count > 0 && indexPath.row == 0) {
            return 60;
        }else{
            return (ScreenWidth-24)/7 + 35;
        }
    }
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    if (section == 1) {
        return 57;
    }
    return 0.1f;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0.1f;
}
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    if (section == 0) {
        return nil;
    }
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, 57)];
    view.backgroundColor = MainBackColor;
    UIImageView *leftFlagImgV = [[UIImageView alloc] initWithFrame:CGRectMake(10, 20, 4, 18)];
    leftFlagImgV.image = LoadImage(@"指南商城_flag");
    [view addSubview:leftFlagImgV];
    UILabel *leftLB = [[UILabel alloc] initWithFrame:CGRectMake(leftFlagImgV.right+5, leftFlagImgV.top-6, 100, 30)];
    leftLB.textColor = MainBlackTextColor;
    leftLB.font = FontSetWithWeight(22, 0.3f);
    leftLB.text = @"好物推荐";
    [view addSubview:leftLB];
    UILabel *detailLB = [[UILabel alloc] initWithFrame:CGRectMake(leftLB.right+10, leftLB.top+6, ScreenWidth/2+30, 18)];
    detailLB.textColor = MainPureColor(153);
    detailLB.font = FontSet(13);
    detailLB.text = @"为您千挑百选 只为精益圆满";
    [view addSubview:detailLB];
    UIImageView *moreImgV = [[UIImageView alloc] initWithFrame:CGRectMake(ScreenWidth - 50, detailLB.top, 18, 18)];
    moreImgV.image = LoadImage(@"指南商城_更多");
    [view addSubview:moreImgV];
    return view;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 1) {
        GuideMallCell *cell = [tableView dequeueReusableCellWithIdentifier:@"GuideMallCell"];
        if (!cell) {
            cell = [[GuideMallCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"GuideMallCell"];
        }
        cell.model = self.dataSourceArr[indexPath.row];
        return cell;
    }else{
        if (self.goodTypesArr.count > 0 && indexPath.row == 0) {
            GoodTypesCell *cell = [tableView dequeueReusableCellWithIdentifier:@"GoodTypesCell"];
            if (!cell) {
                cell = [[GoodTypesCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"GoodTypesCell"];
            }
            cell.sourceArr = self.goodTypesArr;
            [cell setTouchBlock:^(NSInteger touchIndex) {
            }];
            return cell;
        }else{
            InviteFriendImgCell *cell = [tableView dequeueReusableCellWithIdentifier:@"InviteFriendImgCell"];
            if (!cell) {
                cell = [[InviteFriendImgCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"InviteFriendImgCell"];
            }
            return cell;
        }
    }
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
}
#pragma mark - SEL
//菜单
-(void)showMenuClick{
}
#pragma mark - UIScrollViewDelegate
-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
    if (scrollView.contentOffset.y > kHeight_NavBar) {
        self.topNavView.alpha = 1;
    }else if (scrollView.contentOffset.y <= 0 ){
        self.topNavView.alpha = 0;
    }else{
        self.topNavView.alpha = scrollView.contentOffset.y/kHeight_NavBar;
    }
}





@end





